package net.tplusplus.drools.speedtest.facts;

import java.util.Date;

import net.tplusplus.rules.runner.rao.Fact;

/**
 * Each bank transaction must fall within an AccountingPeriod - which
 * is normally a quarter.  A separate AccountingPeriod fact is passed to the
 * Drools engine for each accounting period for which we are processing 
 * calculations.
 * 
 * @author DunningJ
 *
 */
public class AccountingPeriod implements Fact, Comparable<AccountingPeriod>
{
	private Date start;
	private long startl;
	private Long startL;
	private Date end;
	private long endl;
	private Long endL;
	
	public AccountingPeriod(){}
	
	public AccountingPeriod(Date start, Date end)
	{
		this.start = start;
		this.startl = start.getTime();
		this.startL = start.getTime();
		this.end = end;
		this.endl = end.getTime();
		this.endL = end.getTime();
	}

	public Date getStart()
	{
		return start;
	}
	
	public long getStartl()
	{
		return startl;
	}
	
	public long getStartL()
	{
		return startL;
	}

	public void setStart(Date start)
	{
		this.start = start;
	}

	public Date getEnd()
	{
		return end;
	}

	public long getEndl()
	{
		return endl;
	}
	
	public Long getEndL()
	{
		return endL;
	}

	public void setEnd(Date end)
	{
		this.end = end;
	}
	
	/**
	 *  This method enables the Collections api to sort the 
	 *  AccountingPeriod by period start date.
	 */
	public int compareTo(AccountingPeriod accountingPeriod)
	{
		if(this.start.after(accountingPeriod.getStart()))
		{
			return 1;
		}
		if(this.start.before(accountingPeriod.getStart()))
		{
			return -1;
		}	
		
		return 0;
	}	
	
	/**
	 *  This method enables the comparison of AccountingPeriod objects
	 */	
	@Override
	public boolean equals(Object obj) 
	{
		if(obj==null || !(obj instanceof AccountingPeriod))
		{
			return false;
		}
		
		AccountingPeriod ap = (AccountingPeriod) obj;
		
		return getStart().equals(ap.getStart());
	}

	public String toString()
	{
		return "AccountingPeriod[" 
				+"start="+start
				+",end="+end
				+"]";
	}
}
